package com.tepestech.trpg.工具

import org.java_websocket.client.WebSocketClient
import org.java_websocket.drafts.Draft
import org.java_websocket.framing.Framedata
import org.java_websocket.handshake.ServerHandshake
import org.xutils.common.util.LogUtil

import java.net.URI


/**
 * Created by Draculea on 2017/4/10.
 * 实现websocket连接
 */

class WebSocketl连接 : WebSocketClient {
    constructor(uri: URI) : super(uri) {}

    constructor(serverUri: URI, draft: Draft) : super(serverUri, draft) {}

    override fun onOpen(serverHandshake: ServerHandshake) {
        LogUtil.w("opened connection")
    }

    override fun onMessage(s: String) {
        LogUtil.w("received: " + s)
    }

    override fun onFragment(fragment: Framedata?) {
        LogUtil.w("received fragment: " + String(fragment!!.payloadData.array()))
    }

    override fun onClose(i: Int, s: String, b: Boolean) {
        // The codecodes are documented in class org.java_websocket.framing.CloseFrame
        LogUtil.w("Connection closed by " + if (b) "remote peer" else "us")
    }

    override fun onError(e: Exception) {
        e.printStackTrace()
        // if the error is fatal then onClose will be called additionally
    }
}
